//
//  DateExtension.swift
//  PassVPN
//
//  Created by yiwanjun on 2018/1/18.
//  Copyright © 2018年 Maynard. All rights reserved.
//

import Foundation

public extension Date {
    
    /// 判断是不是同一天，默认使用系统的local
    /// - Parameter date:待比较的date
    /// - Returns: bool，true 代表是同一天，否则不是
    func isSameDay(date: Date) -> Bool {
        return Calendar.current.isDate(self, inSameDayAs: date)
    }
    /// 判断是不是同一月，默认使用系统的local
    /// - Parameter date: 待比较的date
    /// - Returns: bool，true 代表是同一月，否则不是
    func isSameMonth(date: Date) -> Bool {
        return Calendar.current.isDate(self, equalTo: date, toGranularity: .month)
    }
    
    /// 格式化的DateString,只有 date，分隔符是斜线 /
    var formatterString: String {
        get {
            let dateFormatter = DateFormatter()
            dateFormatter.timeStyle = .none
            dateFormatter.dateStyle = .short
            let dateStr = dateFormatter.string(from: self)
            return dateStr
        }
    }
    
    //判断时间格式
    func recentlyTimeString() -> String{
        let timeInterval = self.timeIntervalSince1970
        let createDate = Int(timeInterval * 1000)
        
        var result = ""
        
        //当前时间的时间戳
        let nowTimeInterval: TimeInterval = Date().timeIntervalSince1970
        let timeStamp = Int(nowTimeInterval * 1000)
        
        let timeNumber = (timeStamp - createDate) / 1000
        if  timeNumber <= 59{
            result = "\(timeNumber)秒前"
        }else if timeNumber >= 60 && timeNumber < 3600{
            
             result = "\(timeNumber/60)分钟前"
        }else if timeNumber >= 3600 && timeNumber < 86400{
            
             result = "\(timeNumber/3600)小时前"
        }else{
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "yyyy-MM-dd"
            result = dateFormatter.string(from: self)
        }
        return result
    }
}





